package com.gpoint.rdp.door.TimeSegment.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.alibaba.fastjson.JSONObject;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.door.TimeSegment.service.TimeSegService;
import com.gpoint.rdp.door.user.persistence.entity.CampusCardPo;
import com.gpoint.rdp.door.TimeSegment.service.TimeSegQueryService;
import com.gpoint.rdp.door.TimeSegment.persistence.dao.TimeSegQueryDao;
import com.gpoint.rdp.door.TimeSegment.persistence.entity.TimeSegPo;
import com.gpoint.rdp.base.core.util.BeanUtils;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.ArrayList;
import java.util.List;

// TODO: Auto-generated Javadoc
/**
 * TB_DOOR_TIMESEG 控制类
 *
 * <pre>
 *  
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-10-30 09:55:55
 * </pre>
 */
@Controller
@RequestMapping("/rdp/TimeSegment/timeSeg/")
public class TimeSegController extends GenericController {

	/** The time seg service. */
	@Resource
	private TimeSegService timeSegService;

	/** The time seg query service. */
	@Resource
	private TimeSegQueryService timeSegQueryService;


	/**
	 * List.
	 *
	 * @param request  the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = getAutoView();
		return mv;

	}

	/**
	 * a copy of listview in order to expand time segment view, get devive pass
	 * argument
	 *
	 * @param request  the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("list2")
	public ModelAndView list2(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView("/rdp/TimeSegment/timeSegListDetail.jsp");
		String sn = RequestUtil.getString(request, "sn");
         mv.addObject("sn", sn);
		return mv;

	}

	/**
	 * Querysno.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("querytime")
	public @ResponseBody PageJson querysno(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		try {

			// 获取前台数值
			QueryFilter queryFilter = getQuerFilter(request);

			String ctl = RequestUtil.getString(request, "ctl");

			String sql = "TIMESEG ='" + ctl + "'";
			queryFilter.addParamsFilter("whereSql", sql);

			// 调用接口查询
			String listData = timeSegQueryService.query(queryFilter);

			// 返回结果集合

			PageList<TimeSegPo> timeSegList = null;
			if (JacksonUtil.isJsonObject(listData)) {
				List<TimeSegPo> list = TimeSegPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				timeSegList = new PageList<TimeSegPo>(list, pageResult);
			}

			return new PageJson(timeSegList);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * Gets the tree data.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the tree data
	 * @throws Exception the exception
	 */
	@RequestMapping("getData")
	public @ResponseBody List<TimeSegPo> getTreeData(HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		try {
			// 查询小于256 的数据
			String num = "256";
			List<TimeSegPo> list = this.timeSegQueryService.getvalidrecord(num);
          
			if (BeanUtils.isEmpty(list)) {
				list = new ArrayList<TimeSegPo>();
			}

			return list;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 【TB_DOOR_TIMESEG】列表(分页条件查询)数据.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		QueryFilter queryFilter = getQuerFilter(request);

		String num = "256";
		String sql = "TIMESEG <" + num + "";
		queryFilter.addParamsFilter("whereSql", sql);

		try {
			String listData = timeSegQueryService.query(queryFilter);
			PageList<TimeSegPo> timeSegList = null;
			if (JacksonUtil.isJsonObject(listData)) {
				List<TimeSegPo> list = TimeSegPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				timeSegList = new PageList<TimeSegPo>(list, pageResult);
			}

			return new PageJson(timeSegList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}

	/**
	 * 编辑【TB_DOOR_TIMESEG】信息页面.
	 *
	 * @param request  the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String preUrl = RequestUtil.getPrePage(request);
		String id = RequestUtil.getString(request, "id");
		TimeSegPo timeSeg = null;
		if (StringUtil.isNotEmpty(id)) {
			String data = null;
			data = timeSegQueryService.get(id);
			timeSeg = TimeSegPo.fromJsonString(data);
		}

		return getAutoView().addObject("timeSeg", timeSeg).addObject("returnUrl", preUrl);
	}

	/**
	 * 编辑【TB_DOOR_TIMESEG】信息页面.
	 *
	 * @param request  the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String preUrl = RequestUtil.getPrePage(request);
		String id = RequestUtil.getString(request, "id");
		TimeSegPo timeSeg = null;
		if (StringUtil.isNotEmpty(id)) {
			String data = null;
			data = timeSegQueryService.get(id);
			timeSeg = TimeSegPo.fromJsonString(data);
		}
		return getAutoView().addObject("timeSeg", timeSeg).addObject("returnUrl", preUrl);
	}

	/**
	 * 【TB_DOOR_TIMESEG】明细页面.
	 *
	 * @param request  the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String preUrl = RequestUtil.getPrePage(request);
		String id = RequestUtil.getString(request, "id");
		TimeSegPo timeSeg = null;
		if (StringUtil.isNotEmpty(id)) {
			String data = null;
			data = timeSegQueryService.get(id);
			timeSeg = TimeSegPo.fromJsonString(data);
		}
		return getAutoView().addObject("timeSeg", timeSeg).addObject("returnUrl", preUrl);
	}

	/**
	 * 
	 * 保存【TB_DOOR_TIMESEG】信息.
	 *
	 * @param request  the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultMessage message = null;
		try {
			String json = RequestUtil.getString(request, "json");
			// 解析出segment 数值
			JSONObject jsonObject = JSONObject.parseObject(json);
			String timeseg = (String) jsonObject.get("timeseg");
			String id = (String) jsonObject.get("id");
			// 2，查询数记录是否存在，存在不插入返回,不存在插入


			
			//called serviced// create time is null or// UPDATE_TIME_ is not null
			List<TimeSegPo> list =  this.timeSegQueryService.getinsertrecord(timeseg);
			if (null == list || list.size() == 0 || list.get(0).getId().equals(id)) { 
																						
				timeSegService.save(json);
				message = new ResultMessage(ResultMessage.SUCCESS, "保存成功");

			} else {

				message = new ResultMessage(ResultMessage.FAIL, "时段号已经存在！");

			}

		} catch (Exception e) {
			message = new ResultMessage(ResultMessage.FAIL, "操作失败," + e.getMessage());
			logger.error("对TB_DOOR_TIMESEG操作失败，" + e.getMessage(), e);
		}
		writeResultMessage(response.getWriter(), message);
	}

	/**
	 * 批量删除【TB_DOOR_TIMESEG】记录.
	 *
	 * @param request  the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultMessage message = null;
		try {
			// 获得待删除的id
			String[] ids = RequestUtil.getStringAryByStr(request, "id");
			timeSegService.deleteByIds(ids);
			message = new ResultMessage(ResultMessage.SUCCESS, "删除成功");
		} catch (Exception e) {
			message = new ResultMessage(ResultMessage.FAIL, "删除失败，" + e.getMessage());
			logger.error("删除TB_DOOR_TIMESEG失败，" + e.getMessage(), e);
		}
		writeResultMessage(response.getWriter(), message);
	}

}